var urls = {
    validateVerifyCode: '/verify/validate',
    register: '/front/user/_register',
    loginPage: '/login.html',
    verifyCodeImage: '/verify/code'
};

window.layModules = {};

function RegisterForm() {
    this.init();
}

RegisterForm.prototype = {
    init: function () {
        this.form = $('#register-form');
        this.verifyCodeImage = $('#verify-code-image');
        this.initForm();
    },
    initForm: function () {
        var _this = this;
        this.form.on('submit', function (e) {
            e.preventDefault();
            _this.onSubmit(this);
            return false;
        });
    },
    onSubmit: function (form) {
        var _this = this;
        var postData = {
            name: form.name.value,
            password: form.password.value,
            rePassword: form.rePassword.value,
            email: form.email.value,
            mobileNumber: form.mobileNumber.value,
            verifyCode: form.verifyCode.value
        };
        this.validateVerifyCode(postData.verifyCode, function (r) {
            if (r) {
                _this.validateForm(postData);
            } else {
                layModules.layer.msg('验证码错误');
            }
        });
    },
    validateVerifyCode: function (code, cb) {
        if (code.length !== 4) {
            return cb ? cb(false) : false;
        }
        $.get(urls.validateVerifyCode, {code: code}, function (res) {
            return cb ? cb(res.code === 0) : false;
        }, 'json');
    },
    validateForm: function (data) {
        var layer = layModules.layer;
        data.name = data.name.trim();
        if (!/^\w{4,8}$/.test(data.name)) {
            layer.msg('请输入4-8位用户名');
            return;
        }

        data.password = data.password.trim();
        if (!/^\w{6,16}$/.test(data.password)) {
            lay.msg('请输入6-16位密码')
            return;
        }
        if (data.password !== data.rePassword) {
            layer.msg('两次输入密码不一致');
            return;
        }
        if (!/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(data.email)) {
            layer.msg('请输入正确的邮箱');
            return;
        }
        if (data.mobileNumber) {
            if (!/^1([34578])\d{9}$/.test(data.mobileNumber)) {
                layer.msg('请输入正确的手机号码');
                return;
            }
        }
        this.submitRegister(data);
    },
    submitRegister: function (data) {
        var _this = this;
        $.post(urls.register, data, function (res) {
            console.log(res);
            if (res.code === 0) {
                layModules.layer.msg('注册成功');
                setTimeout(function () {
                    window.location.href = urls.loginPage;
                }, 1000);
            } else {
                layModules.layer.msg(res.msg);
                _this.verifyCodeImage.attr('src', urls.verifyCodeImage + '?' + new Date().getTime());
            }
        }, 'json');
    }
};

$(function () {
    var onPlatformReady = function () {
        new RegisterForm();
    };
    layui.use(['form', 'layer', 'element'], function () {
        layModules.from = layui.from;
        layModules.layer = layui.layer;
        layModules.element = layui.element;
        onPlatformReady();
    });
});